package rabbitMQUtils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 项目通用工具类
 * 静态变量可使用配置文件注入
 */

public class RabbitMQUtil {

    //远程服务器ip
    private final static String REMOTE_IP = "59.110.164.26";

    //RabbitMQ远程登录用户
    private final static String USER_NAME = "gejingyu";

    //RabbitMQ远程登录密码
    private final static String PASSWORD = "g19991227";

    /**
     * 根据给定的远程访问信息，创建RabbitMQ连接和信道
     *
     * @return 信道
     * @throws IOException      创建TCP连接和信道时发生的IO异常
     * @throws TimeoutException 创建TCP连接时发生的超时异常
     */
    public static Channel getChannel() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(REMOTE_IP);
        factory.setUsername(USER_NAME);
        factory.setPassword(PASSWORD);
        Connection connection = factory.newConnection();
        return connection.createChannel();
    }
}
